import qs from 'qs'

export default {
  components: {
    'indexAdd': () => import('./index/add'),
    'indexModify': () => import('./index/modify'),
    'indexView': () => import('./index/view')
  },
  data() {
    return {
      commonMap: {
        formShowFullVisible: false,
        formSubmitLoading: false,
        deletesSubmitLoading: false,
        deleteSubmitLoading: false
      },
      form: {
        ${(businessCode?uncap_first)!''}Name: '',
        ${(businessCode?uncap_first)!''}Code: '',
        createdDates: [],
        pageSize: 20,
        pageNumber: 1,
        fuzzySearch: true
      },
      formCreatedDateOptions: {
        shortcuts: [{
          text: '最近7天',
          onClick(picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, new Date()])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, new Date()])
          }
        }, {
          text: '最近90天',
          onClick(picker) {
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, new Date()])
          }
        }]
      },
      pageTableHeight: 200,
      page: {
        content: [],
        totalElements: 0,
        totalPages: 0,
        hasPrevious: true,
        previous: 1,
        hasNext: true,
        next: 2,
        pages: [
          1,
          2,
          3,
          4,
          5,
          6
        ],
        page: 0,
        pageSize: 25
      },
      pageSelection: []
    }
  },
  computed: {
    formQueryString() {
      let query = {}

      for (let key in this.form) {
        query[key] = this.form[key]
      }

      query.pageSize = query.pageSize * 1000

      return qs.stringify(query, { arrayFormat: 'repeat' })
    },
    permissionAdd() {
      if (this.$store.getters['security/hasRole'](['administrator'])) {
        return true
      }

      if (this.$store.getters['security/isPermitted'](['/${moduleMapperCode}/${(businessCode?uncap_first)!''}:add'])) {
        return true
      }

      return false
    },
    permissionDelete() {
      if (this.$store.getters['security/hasRole'](['administrator'])) {
        return true
      }

      if (this.$store.getters['security/isPermitted'](['/${moduleMapperCode}/${(businessCode?uncap_first)!''}:delete'])) {
        return true
      }

      return false
    },
    permissionEdit() {
      if (this.$store.getters['security/hasRole'](['administrator'])) {
        return true
      }

      if (this.$store.getters['security/isPermitted'](['/${moduleMapperCode}/${(businessCode?uncap_first)!''}:edit'])) {
        return true
      }

      return false
    },
    permissionView() {
      if (this.$store.getters['security/hasRole'](['administrator'])) {
        return true
      }

      if (this.$store.getters['security/isPermitted'](['/${moduleMapperCode}/${(businessCode?uncap_first)!''}:view'])) {
        return true
      }

      return false
    },
    permissionImport() {
      if (this.$store.getters['security/hasRole'](['administrator'])) {
        return true
      }

      if (this.$store.getters['security/isPermitted'](['/${moduleMapperCode}/${(businessCode?uncap_first)!''}:import'])) {
        return true
      }

      return false
    },
    permissionExport() {
      if (this.$store.getters['security/hasRole'](['administrator'])) {
        return true
      }

      if (this.$store.getters['security/isPermitted'](['/${moduleMapperCode}/${(businessCode?uncap_first)!''}:export'])) {
        return true
      }

      return false
    }
  },
  created() {
  },
  methods: {
    formSubmitClick(pageNumber) {
      if (pageNumber) {
        this.form.pageNumber = pageNumber
      }

      this.$http({
        url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/page/index',
        method: 'post',
        data: this.form,
        thenRequest: () => {
          this.commonMap.formSubmitLoading = true
        },
        thenResponse: () => {
          this.commonMap.formSubmitLoading = false
        }
      }).then(response => {
        this.page = response.data

        this.pageSelection = []

        this.pageTableHeight = window.innerHeight - this.$refs.pageTable.$el.offsetTop - 42
      })
    },
    formResetSubmitClick() {
      this.$refs.form.resetFields()
    },
    ${(businessCode?uncap_first)!''}FetchSuggestions(queryString, cb) {
      this.$http({
        url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/list/options',
        method: 'post',
        data: { term: queryString }
      }).then(response => {
        cb(response.data)
      })
    },
    deletesSubmitClick() {
      if (!this.pageSelection || this.pageSelection.length < 1) {
        this.$message({
          type: 'warning',
          message: '未勾选${businessName!}数据'
        })

        return
      }

      this.$confirm('将删除${businessName!}数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let ${(businessCode?uncap_first)!''}Ids = this.pageSelection.map(function (row, index, array) {
          return row.${(businessCode?uncap_first)!''}Id
        }).join(',')

        this.$http({
          url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/removeByIds',
          method: 'post',
          data: { ${(businessCode?uncap_first)!''}Ids: ${(businessCode?uncap_first)!''}Ids },
          thenRequest: () => {
            this.commonMap.deletesSubmitLoading = true
          },
          thenResponse: () => {
            this.commonMap.deletesSubmitLoading = false
          },
          successMessage: '删除${businessName!}数据成功',
          errorMessage: '删除${businessName!}数据失败：'
        }).then(response => {
          this.formSubmitClick()
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除${businessName!}数据'
        })
      })
    },
    importBeforeUpload(file) {
      this.commonMap.importSubmit = true

      return true
    },
    importSuccess(data, file, fileList) {
      this.$refs.import.clearFiles()

      this.commonMap.importSubmit = false

      if (data.errorCode) {
        this.$message({
          type: 'error',
          message: data.error
        })

        return
      }

      this.$message({
        type: 'success',
        message: '导入${businessName!}数据成功'
      })
    },
    importError(err, file, fileList) {
      this.$message({
        type: 'error',
        message: err.message
      })

      this.$refs.import.clearFiles()

      this.commonMap.importSubmit = false
    },
    pageSelectionChange(val) {
      this.pageSelection = val
    },
    viewSubmitClick(row) {
      this.$refs.indexView.form.${(businessCode?uncap_first)!''}Id = row.${(businessCode?uncap_first)!''}Id

      this.$refs.indexView.dialogVisible = true
    },
    modifySubmitClick(row) {
      this.$refs.indexModify.form.${(businessCode?uncap_first)!''}Id = row.${(businessCode?uncap_first)!''}Id

      this.$refs.indexModify.dialogVisible = true
    },
    deleteSubmitClick(row) {
      this.$confirm('将删除${businessName!}数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: '${api_root_url!}/v1/${moduleMapperCode}/${(businessCode?uncap_first)!''}/removeById',
          method: 'post',
          data: { ${(businessCode?uncap_first)!''}Id: row.${(businessCode?uncap_first)!''}Id },
          thenRequest: () => {
            this.commonMap.deleteSubmitLoading = true
          },
          thenResponse: () => {
            this.commonMap.deleteSubmitLoading = false
          },
          successMessage: '删除${businessName!}数据成功',
          errorMessage: '删除${businessName!}数据失败：'
        }).then(response => {
          this.formSubmitClick()
        })
      }).catch(() => {
        this.$message({
          type: 'warning',
          message: '已取消删除${businessName!}数据'
        })
      })
    }
  }
}
